import { fileURLToPath, URL } from 'node:url'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import ElementPlus from 'unplugin-element-plus/vite'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import Inspect from 'vite-plugin-inspect'

export default defineConfig(({ mode }) => {
  // const env = loadEnv(mode, process.cwd())
  const env = loadEnv(mode, 'env', '')
  return {
    envDir: 'env',
    plugins: [
      vue(),
      vueJsx(),
      // vueDevTools(),

      AutoImport({
        imports: ['vue', 'vue-router', 'pinia', '@vueuse/core', 'vue-i18n'],
        resolvers: [ElementPlusResolver()],
        dts: 'src/types/auto-generated/auto-imports.d.ts',
        dirs: [
          'src/composables/**',
          'src/stores/**',
          'src/utils/**',
          'src/services/**',
          'src/types/**',
        ],

        // 忽略eslint检查
        eslintrc: {
          enabled: true,
          filepath: './.eslintrc-auto-import.json',
        },
      }),

      Components({
        resolvers: [
          ElementPlusResolver({
            importStyle: 'sass',
            directives: true, // 自动导入指令
          }),
          IconsResolver({
            prefix: 'Icon', // 添加图标前缀
            enabledCollections: ['ep', 'mdi', 'fa', 'ic'],
          }),
        ],
        // directoryAsNamespace: true, // 允许子目录作为命名空间
        dts: 'src/types/auto-generated/components.d.ts',
        dirs: ['src/components', 'src/views'],
        deep: true,
      }),

      Icons({
        autoInstall: true,
        compiler: 'vue3',
      }),
      Inspect({
        enabled: mode !== 'production',
      }),
      ElementPlus({
        useSource: true,
        // defaultLocale: 'en-us',
      }),
    ],

    // 代理服务器配置
    server: {
      // port: 5000,
      open: true,
      secure: false,
      proxy: {
        '/api': {
          target: env.VITE_API_BASE_URL,
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, ''),
        },
      },
    },

    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      },
    },

    esbuild: {
      pure: mode === 'production' ? ['console.log', 'console.warn'] : [],
      // legalComments: 'none',
      // treeShaking: true,
      // minifySyntax: true,
      // minifyWhitespace: true,
      // minifyIdentifiers: true,
    },

    // 构建配置
    build: {
      target: 'es2020',
      outDir: 'dist',
      assetsDir: 'assets',
      cssCodeSplit: true,
      sourcemap: mode !== 'production',

      // 分包配置
      rollupOptions: {
        output: {
          manualChunks: {
            'vue-vendor': ['vue', 'vue-router', 'pinia'],
            'element-plus': ['element-plus'],
          },
        },
      },
    },

    // CSS 配置
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/styles/element-plus/index.scss" as *; @use "@/styles/variables.scss" as *;`,
        },
      },
    },

    // 性能优化
    optimizeDeps: {
      include: ['vue', 'vue-router', 'pinia', '@vueuse/core', 'element-plus'],
    },
  }
})
